package com.shiguo365.market.display.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.shiguo365.market.R;
import com.shiguo365.market.display.adapter.OrderAdapter;
import com.shiguo365.market.bean.Product;
import com.shiguo365.market.display.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OrderConfirmActivity extends BaseActivity {

    private List<Product> productList=new ArrayList<Product>();

    @BindView(R.id.buttonOrderConfirm)
    protected Button orderConfirmButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_confirm);
        initProduct();
        OrderAdapter adapter = new OrderAdapter(this,R.layout.item_order,productList);
        ListView listView1 =(ListView) findViewById(R.id.listViewOrder);
        listView1.setAdapter(adapter);
        setListViewHeightBasedOnChildren(listView1);
        setTitle("订单创建");
        ButterKnife.bind(this);
    }


    @OnClick(R.id.buttonOrderConfirm)
    public void orderConfirm(){
        Intent intent=new Intent(this, OrderPayActivity.class);
        startActivity(intent);
    }

    //初始化商品列表模拟
    private void initProduct(){
        Product product = new Product();
        product.setId(1);
        product.setName("海南菠萝蜜");
        product.setContext("海南菠萝蜜的介绍");
        product.setPrice(189.22);
        product.setSpecname("约23斤每箱");
        product.setImgurl("http://7xj380.com1.z0.glb.clouddn.com/2016-05-27_574828de4587e.png");
        productList.add(product);
        product = new Product();
        product.setId(2);
        product.setName("美国JJ级车厘子");
        product.setContext("美国JJ级车厘子的介绍");
        product.setPrice(500.00);
        product.setSpecname("约15斤每箱");
        product.setImgurl("http://7xj380.com1.z0.glb.clouddn.com/2016-07-01_57763965ecce4.jpg");
        productList.add(product);
        product = new Product();
        product.setId(3);
        product.setName("新疆金蜜哈密瓜");
        product.setContext("新疆金蜜哈密瓜的介绍");
        product.setPrice(100.00);
        product.setSpecname("约40斤每箱");
        product.setImgurl("http://7xj380.com1.z0.glb.clouddn.com/2016-07-05_577b2b5822232.jpg");
        productList.add(product);
        product = new Product();
        product.setId(4);
        product.setName("智利有籽红提");
        product.setContext("智利有籽红提的介绍");
        product.setPrice(100.00);
        product.setSpecname("约20斤每箱");
        product.setImgurl("http://7xj380.com1.z0.glb.clouddn.com/2016-05-29_574ab09d4c8f1.png");
        productList.add(product);


    }

    /**
     * 动态设置ListView的高度
     * @param listView
     */
    public static void setListViewHeightBasedOnChildren(ListView listView) {
        if(listView == null) return;
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }
        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }
}
